package whr.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import whr.common.utils.DateUtils;
import whr.common.utils.PageUtils;
import whr.common.utils.Query;
import whr.modules.assets.entity.InfoEntity;
import whr.modules.sys.dao.SysOrganizationsMapper;
import whr.modules.sys.entity.SysOrganizations;
import whr.modules.sys.service.SysOrganizationsService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LENOVO
 * @description 针对表【sys_organizations】的数据库操作Service实现
 * @createDate 2024-09-23 16:23:40
 */
@Service
public class SysOrganizationsServiceImpl extends ServiceImpl<SysOrganizationsMapper, SysOrganizations> implements SysOrganizationsService {

    @Autowired
    private SysOrganizationsMapper sysOrganizationsMapper;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String) params.get("name");
//        System.out.println("Received name: " + name);

        // 确保 name 不为空或空白
        if (StringUtils.isNotBlank(name)) {
            System.out.println("Name is not blank: " + name);
        } else {
            System.out.println("Name is blank or null.");
        }
        String time=(String) params.get("createTime");
        System.out.println("time"+time);
        IPage<SysOrganizations> page = this.page(
                new Query<SysOrganizations>().getPage(params),
                new QueryWrapper<SysOrganizations>()
                        .like(StringUtils.isNotBlank(name),"name",name)


        );
//         打印最终生成的 SQL 语句
//        String sql = new QueryWrapper<SysOrganizations>()
//                .like(StringUtils.isNotBlank(name), "name", name)
//                .getSqlSegment();
//        System.out.println("Generated SQL: " + sql);
//
//         打印返回的数据
//        System.out.println("Returning data: " + page.getRecords());
//        System.out.println("Total count: " + page.getTotal());
        return new PageUtils(page);

    }


    // 根据id查询单个部门
    @Override
    public SysOrganizations getById(Integer id) {
        return sysOrganizationsMapper.seleteById(id);
    }

    // 添加部门
    @Override
    public boolean saveBySysOrganizations(SysOrganizations sysOrganizations) {
        sysOrganizations.setCreateTime(DateUtils.now());
        System.out.println("创建时间"+sysOrganizations.getCreateTime());
        sysOrganizations.setStatus(1);
        sysOrganizations.setUpdateTime(DateUtils.now());
        int insert = sysOrganizationsMapper.insertOrg(sysOrganizations);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    // 根据部门名称查询部门
    @Override
    public boolean getByName(String name) {
        List<SysOrganizations> organizations = sysOrganizationsMapper.selectByName(name);
        if (organizations != null && !organizations.isEmpty()) {
            return true;
        }
        return false;
    }

    // 逻辑删除部门
    @Override
    public void deleteByIds(List<Integer> ids) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("id",ids);
        SysOrganizations sysOrganizations=new SysOrganizations();
//        sysOrganizations.setIsDelete(Compents.IS_DELETE);
        sysOrganizationsMapper.update(sysOrganizations,wrapper);
    }

    // 真删除批量删除部门
    @Override
    public void deleteBatch(Integer[] ids) {
        this.removeByIds(Arrays.asList(ids));
    }


    @Override
    public List<SysOrganizations> selectByLikeName(String name) {

        return  sysOrganizationsMapper.selectByLikeName(name);
    }




}




